﻿using Microsoft.SharePoint.Client;
using System;
using System.Net;

namespace Office365.SharePointOnline.Cmdlets
{
    [System.Management.Automation.Cmdlet(System.Management.Automation.VerbsCommon.Get, "ContenttypeUsage")]
    public class Get_ContenttypeUsage : System.Management.Automation.PSCmdlet
    {
        [System.Management.Automation.Parameter(Position = 0, Mandatory = true)]
        public string Url;

        [System.Management.Automation.Parameter(Position = 1, Mandatory = false)]
        public string User;

        [System.Management.Automation.Parameter(Position = 2, Mandatory = false)]
        public string Password;

        [System.Management.Automation.Parameter(Position = 3, Mandatory = false)]
        public string AuthenticationMethod;

        // use only if onpremise authentication
        [System.Management.Automation.Parameter(Position = 4, Mandatory = false)]
        public string Domain;

        [System.Management.Automation.Parameter(Position = 5, Mandatory = false)]
        public string ContentTypeName;

        protected override void ProcessRecord()
        {
            // check parameters
            ClientContext context = new ClientContext(Url);
            ICredentials credentials;
            Web web = context.Web;
            this.WriteVerbose("Logging on.");
            if (string.IsNullOrEmpty(Url))
            {
                this.WriteObject("Error: url is not set");
            }
            else
            {
                if (!string.IsNullOrEmpty(Password) && !string.IsNullOrEmpty(User))
                {
                    AuthenticationHelper authenticationHelper = new AuthenticationHelper() { AuthenticationMethod = AuthenticationMethod, Domain = Domain, Password = Password, User = User };
                    credentials = authenticationHelper.GetCredentials();
                    if (credentials != null)
                    {
                        context.Credentials = credentials as ICredentials;
                    }
                    else
                    {
                        throw new NullReferenceException("No credentials returned.");
                    }
                }
                else
                {
                    this.WriteVerbose("Reusing existing credentials");
                }

                this.WriteVerbose("Getting contenttype usage.");
                context.Load(web);
                context.ExecuteQuery();

                GetContenttypeUsage(context, web);
            }
        }

        private void GetContenttypeUsage(ClientContext context, Web web)
        {
            var webs = web.Webs;
            context.Load(webs);
            context.ExecuteQuery();
            foreach (var subweb in webs)
            {
                context.Load(subweb);
                context.ExecuteQuery();
                var lists = subweb.Lists;
                context.Load(lists);
                context.ExecuteQuery();
                foreach (var list in lists)
                {
                    context.Load(list);
                    context.ExecuteQuery();
                    var cts = list.ContentTypes;
                    context.Load(cts);
                    context.ExecuteQuery();
                    foreach (var ct in cts)
                    {
                        context.Load(ct);
                        context.ExecuteQuery();
                        if (string.IsNullOrEmpty(ContentTypeName))
                        {
                            this.WriteObject(string.Format("Web: {0} List: {1} Contenttype {2}.", subweb.Url, list.Title, ct.Name));
                        }
                        else if (ct.Name == ContentTypeName)
                        {
                            this.WriteObject(string.Format("Web: {0} List: {1} Contenttype {2}.", subweb.Url, list.Title, ct.Name));
                        }
                    }
                }
            }
        }
    }
}
